var Tabs = function( id ) {
	this.setId = function( id ) {
		this.id_ = id;
	};

	this.getId = function() {
		return this.id_;
	};

	this.setPane = function( pane ) {
		this.pane_ = pane;
	};

	this.getPane = function() {
		return this.pane_;
	};

	this.setPicker = function( picker ) {
		this.picker_ = picker;
	};

	this.getPicker = function() {
		return this.picker_;
	};

	this.setArea = function( area ) {
		this.area_ = area;
	};

	this.getArea = function() {
		return this.area_;
	};

	this.setEnabled = function( enabled ) {
		this.enabled_ = enabled;
	};

	this.getEnabled = function() {
		return this.enabled_;
	};

	this.components_ = function( id ) {
		var thiz = this;
		this.setPane( Util.id( id ) );
		Util.kids( 
			this.getPane()
			, function( node ) { return Util.hasClass( node, 'tabs_picker' ) }
			, function( node ) { thiz.setPicker( node ); }
		);
		Util.kids( 
			this.getPane()
			, function( node ) { return Util.hasClass( node, 'tabs_area' ) }
			, function( node ) { thiz.setArea( node ); }
		);
	};

	this.enableTabByLabel = function( label ) {
		this.enableTab( this.getTabByLabel( label ) );
	};

	this.enableTab = function( node ) { 
		if ( this.getEnabled() ) {
			this.disableTab( this.getEnabled() );
		}

		node.setAttribute( 'class', 'tabs_picker_hot' );
		node.setAttribute( 'className', 'tabs_picker_hot' ); // IE is dumb

		var area = this.getAreaForTab( node );
		if ( area ) {
			Util.show( area );
			Util.visible( area );
		}

		this.setEnabled( node );
	};

	this.disableTab = function( node ) { 
		node.setAttribute( 'class', 'tabs_picker_cold' );
		node.setAttribute( 'className', 'tabs_picker_cold' ); // IE is dumb

		var area = this.getAreaForTab( node );
		if ( area ) {
			Util.hide( area );
			Util.invisible( area );
		}
	};

	this.getTabIndexForTab = function( node ) {
		return node.getAttribute( 'tab_index' );
	};

	this.getAreaIdForTab = function( node ) {
		return this.areaId( this.getTabIndexForTab( node ) );
	};

	this.getAreaByLabel = function( label ) {
		return this.getAreaForTab( this.getTabByLabel( label ) );
	};

	this.getAreaByTab = function( node ) {
		return this.getAreaForTab( node );
	};

	this.getAreaForTab = function( node ) {
		return this.getAreaForTabIndex( node.getAttribute( 'tab_index' ) );
	};

	this.getAreaForTabIndex = function( tab_index ) {
		return Util.id( this.areaId( tab_index ) );
	};

	this.setTabs = function( tabs ) {
		this.tabs_ = tabs;
	};

	this.getTabs = function() {
		return this.tabs_;
	};

	this.setTabCount = function( tabCount ) {
		this.tabCount_ = tabCount;
	};

	this.getTabCount = function() {
		return this.tabCount_;
	};

	this.add = function( label ) {
		var node = Util.newNode( 'li' );
		node.appendChild( Util.newTxt( label ) );
		this.addTab( label, node );
		this.getPicker().appendChild( node );
		this.getArea().appendChild( 
			this.addArea( 
				this.getTabIndexForTab( node )
				, Util.newNode( 'div', {class:'tab'} )
			)
		);
		return node;
	};

	this.getTabByLabel = function( label ) {
		return this.getTabs()[ label ];
	};

	this.addTab = function( label, node ) {
		var thiz = this;
		var tab_index = this.getTabCount();
		node.setAttribute( 'label', label );
		node.setAttribute( 'tab_index', tab_index );
		node.setAttribute( 'class', 'tabs_picker_cold' );
		node.setAttribute( 'className', 'tabs_picker_cold' ); // IE is dum
		node.setAttribute( 'type', 'TXT' );
		this.getTabs()[ label ] = node;
		node.onclick = function( e ) { thiz.enableTab( node ) };
		this.setTabCount( 1 + tab_index );
		return node;
	};

	this.init_tabs = function() {
		var thiz = this;
		Util.kids(
			this.getPicker()
			, function( node ) { return Util.isTag( node, 'LI' ); }
			, function( node ) {
				thiz.addTab( Util.pullText( node ), node );
				if ( 0 == node.getAttribute( 'tab_index' ) ) {
					thiz.enableTab( node );
				} else {
					thiz.disableTab( node );
				}
			}
		);
	};

	this.addArea = function( tab_index, node ) {
		Util.invisible( node );
		Util.hide( node );
		node.setAttribute( 'id', this.areaId( tab_index ) );
		return node;
	};

	this.areaId = function( tab_index ) {
		return this.getId() + '_tab_area_' + tab_index;
	};

	this.init_area = function() {
		var tab_index = 0;
		var thiz = this;
		Util.kids(
			this.getArea()
			, function( node ) { return Util.hasClass( node, 'tab' ); }
			, function( node ) {
				thiz.addArea( tab_index, node );
				if ( 0 == tab_index ) {
					Util.show( node );
					Util.visible( node );
				}
				tab_index++;
			}
		);
	};

	this.manage = function( id ) {
		this.setId( id );
		this.components_( id );
		this.init_tabs();
		this.init_area();
	};

	// "constructor"

	this.init = function( id ) {
		this.setTabs( [] );
		this.setTabCount( 0 );
		if ( id ) {
			this.manage( id );
		}
	};
	
	this.init( id );
};
